package cn.com.bsoft.cloud.utils;

import cn.com.bsoft.cloud.entity.cloud.Content;
import cn.com.bsoft.cloud.entity.cloud.UpdatePayStatus;
import cn.com.bsoft.cloud.entity.cloud.data;
import cn.com.bsoft.cloud.entity.cloud.master;
import cn.com.bsoft.cloud.service.HisInterface;
import cn.com.bsoft.cloud.utils.test.DateUtil;
import cn.com.bsoft.cloud.utils.test.HttpUtilTest;
import cn.com.bsoft.cloud.utils.test.SecuritySHA1Utils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CompanyInterface {
    @Autowired
    private  HisInterface hisInterface;
    //国药接口
    public Map guoYao(UpdatePayStatus updatePayStatus,String companyCode) {
        Map<String,String> resultMap = new HashMap<String, String>();
        List<master> masters = hisInterface.queryCompanyParam(updatePayStatus);
        if (masters == null && masters.size() <= 0) {
            resultMap.put("code", ResultCode.ERROR);
            resultMap.put("message", "未查出药品列表！");
            return resultMap;
        } else {
            Map paramMap = hisInterface.queryAppIdAndToken(companyCode);
            String appId = String.valueOf(paramMap.get("appId"));
            String token = String.valueOf(paramMap.get("token"));
            String url = String.valueOf(paramMap.get("url"));
            try {
                String timess = DateUtil.timeStamp();
                String timestamp = DateUtil.timeStampToDotNet(timess);
                String nonce = "8217";
                String date = DateUtil.timeStamp2Date(timess, "yyyy-MM-dd HH:mm:ss");
                String str = SecuritySHA1Utils.getSignature(appId, timestamp, nonce, token);

                url += "?appid=" + appId + "&timestamp=" + timestamp + "&nonce=" + nonce + "&accesstoken=" + str;
                data data = new data();
                data.setMsgType("MHZF001");
                data.setDataType("Json");
                data.setCreateTime(date);
                Content content = new Content();
                content.setMaster(masters);
                data.setContent(content);

                String postPars = JSON.toJSONString(data);
                postPars="data="+postPars;

                String requestString = HttpUtilTest.HttpPostWithJsonString(url, postPars);

                Map resMap = (Map) JSON.parse(requestString);
                String code = String.valueOf(resMap.get("ErrCode"));
                if ("1".equals(code)) {
                    boolean updateFlag = hisInterface.updatePayStaus(updatePayStatus);
                    if (updateFlag) {
                        resultMap.put("code", ResultCode.SUCCESS);
                        resultMap.put("message", "更改成功！");
                        return resultMap;
                    } else {
                        resultMap.put("code", ResultCode.ERROR);
                        resultMap.put("message", "更改失败！");
                        return resultMap;
                    }
                } else {
                    String message = String.valueOf(resMap.get("Message"));
                    resultMap.put("code", ResultCode.ERROR);
                    resultMap.put("message", "调用医药公司接口失败！报错信息为：" + message);
                    return resultMap;
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultMap.put("code", ResultCode.ERROR);
                resultMap.put("message", e.getMessage());
                return resultMap;
            }
        }
    }
    public Map shangYao(UpdatePayStatus updatePayStatus,String companyCode) {
        Map<String, String> resultMap = new HashMap<String, String>();
        List<master> masters = hisInterface.queryCompanyParam(updatePayStatus);
        for(int k=0;k<masters.size();k++){
            master master= masters.get(k);
            master.setStatus("4");
        }
        if (masters == null && masters.size() <= 0) {
            resultMap.put("code", ResultCode.ERROR);
            resultMap.put("message", "未查出药品列表！");
            return resultMap;
        } else {
            try {
                Map<String, List<master>> shangYaoParam = new HashMap<String, List<master>>();
                shangYaoParam.put("confirmCfData", masters);
                String postPars = JSON.toJSONString(shangYaoParam);
                Map urlMap = hisInterface.queryAppIdAndToken(companyCode);
                String url = String.valueOf(urlMap.get("url"));
                String str = HttpUtilTest.httpPost(url, postPars);
                System.out.println(str);
                Map resMap = (Map) JSON.parse(str);
                String code = String.valueOf(resMap.get("code"));
                if ("1".equals(code)) {
                    boolean updateFlag = hisInterface.updatePayStaus(updatePayStatus);
                    if (updateFlag) {
                        resultMap.put("code", ResultCode.SUCCESS);
                        resultMap.put("message", "更改成功！");
                        return resultMap;
                    } else {
                        resultMap.put("code", ResultCode.ERROR);
                        resultMap.put("message", "更改失败！");
                        return resultMap;
                    }
                } else {
                    String message = String.valueOf(resMap.get("message"));
                    resultMap.put("code", ResultCode.ERROR);
                    resultMap.put("message", "调用医药公司接口失败！报错信息为：" + message);
                    return resultMap;
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultMap.put("code", ResultCode.ERROR);
                resultMap.put("message", e.getMessage());
                return resultMap;
            }
        }
    }
}
